<?php
class WP_Widget_CategoryArchives extends WP_Widget_Archives {
	var $_category;
	
	function widget($args, $instance) {
		global $wp_rewrite;
		if ($instance['category']) {
			$this->_category = $instance['category'];
			$archives_args_filter = array(&$this, '_archives_args_filter');
			add_filter('widget_archives_args', $archives_args_filter);
			add_filter('widget_archives_dropdown_args', $archives_args_filter);
		}
		parent::widget($args, $instance);
		if ($instance['category']) {
			remove_filter('widget_archives_args', $archives_args_filter);
			remove_filter('widget_archives_dropdown_args', $archives_args_filter);
		}
	}
	
	function _archives_args_filter($args) {
		$args['category'] = $this->_category;
		return $args;
	}
	
	function update($new_instance, $old_instance) {
		$instance = parent::update($new_instance, $old_instance);
		$instance['category'] = (int)$new_instance['category'];
		return $instance;
	}
	
	function form($instance) {
		parent::form($instance);
?>
	<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category') ?></label>
	<select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
		<option value="0" <?php selected(0, $instance['category']) ?>><?php _e('Select Category') ?></option>
	<?php foreach (get_categories('hide_empty=0') as $category): ?>
		<option value="<?php echo esc_attr($category->cat_ID) ?>" <?php selected($category->cat_ID, $instance['category']) ?>><?php echo $category->name; ?></option>
	<?php endforeach; ?>
	</select></p>
<?php
	}
}
unregister_widget('WP_Widget_Archives');
register_widget('WP_Widget_CategoryArchives');
